package com.example.anliku.service.serviceImpl;

import com.example.anliku.entity.*;
import com.example.anliku.entity.ExampleSeeBody.ExampleSeeBody;
import com.example.anliku.entity.ExampleSeeBody.ExampleSeeRetBody;
import com.example.anliku.entity.teamBody.TeamMemberBody;
import com.example.anliku.entity.teamBody.TeamMemberReturnBody;
import com.example.anliku.mapper.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.example.anliku.service.ExampleSeeService;

import java.util.*;

@Service
public class ExampleSeeServiceImpl implements ExampleSeeService{

    @Resource
    private ExampleSeeMapper exampleSeeMapper;

    @Resource
    private ExampleMapper exampleMapper;

    @Resource
    private ExampleClazzMapper exampleClazzMapper;

    @Resource
    private ExampleKnowledgeMapper exampleKnowledgeMapper;

    @Resource
    private DownloadMapper downloadMapper;
    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public Message deleteByPrimaryKey(Integer exampleSeeId) {
        return exampleSeeMapper.deleteByPrimaryKey(exampleSeeId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public int insert(ExampleSee record) {
        return exampleSeeMapper.insert(record);
    }

    @Override
    public int insertSelective(ExampleSee record) {
        return exampleSeeMapper.insertSelective(record);
    }

    @Override
    public ExampleSee selectByPrimaryKey(Integer exampleSeeId) {
        return exampleSeeMapper.selectByPrimaryKey(exampleSeeId);
    }

    @Override
    public int updateByPrimaryKeySelective(ExampleSee record) {
        return exampleSeeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ExampleSee record) {
        return exampleSeeMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message insertList(List<ExampleSee> list) {
        return exampleSeeMapper.insertList(list) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByExampleSeeUserId(String exampleSeeUserId) {
        List<Map> mapList = new LinkedList<>();
        List<ExampleSee> exampleList = exampleSeeMapper.selectByExampleSeeUserId(exampleSeeUserId);
        for (ExampleSee exampleSee : exampleList) {
            Integer exampleId = exampleSee.getExampleSeeExampleId();
            Example examplea = exampleMapper.selectByPrimaryKey(exampleId);
            Integer DownloadCase=downloadMapper.countByDownloadCaseId(exampleId.toString());
            Integer CommentCase=commentMapper.countByCommentCaseId(exampleId.toString());
            HashMap map = new HashMap();
            map.put("example", examplea);
            map.put("exampleDownloadNum", DownloadCase);
            map.put("CommentCaseNum", CommentCase);
            List<ExampleKnowledge> knowledgeList = exampleKnowledgeMapper.selectAllByExampleKnowledgeExampleId(exampleId);
            List<ExampleClazz> clazzList = exampleClazzMapper.selectAllByExampleClazzExampleId(exampleId);
            map.put("knowledgeList", knowledgeList);
            map.put("clazzList", clazzList);
            mapList.add(map);
        }
        return Message.success(null).add(mapList);
    }

    @Override
    public Message batchInsert(List<ExampleSeeRetBody> list) {
        List<ExampleSeeBody> exampleSeeBodies=new ArrayList<>();
        for(ExampleSeeRetBody exampleSeeRetBody:list) {
            User user=userMapper.selectByPrimaryKey(exampleSeeRetBody.getExampleSeeUserId());
            if(user == null){
                return Message.fail("用户ID不存在");
            }
            Example example=exampleMapper.selectByPrimaryKey(exampleSeeRetBody.getExampleSeeExampleId());
            if(example == null){
                return Message.fail("案例ID不存在");
            }
            List<ExampleSee>  aaa = exampleSeeMapper.selectAllByExampleSeeUserIdAndExampleSeeExampleId(exampleSeeRetBody.getExampleSeeUserId(), exampleSeeRetBody.getExampleSeeExampleId());
            if (CollectionUtils.isNotEmpty(aaa)) {
                return Message.fail("案例可查看人添加重复重复");
            }
            exampleSeeBodies.add(ExampleSeeBody.get(exampleSeeRetBody));
        }
        return exampleSeeMapper.batchInsert(exampleSeeBodies) > 0 ? Message.success(null) : Message.fail(null);
    }


}
